package advertising.boost

import org.apache.commons.lang3.StringUtils

//  .--,       .--,
// ( (  \.---./  ) )
//  '.__/o   o\__.'
//     {=  ^  =}
//      >  -  <
//     /       \
//    //       \\
//   //|   .   |\\
//   "'\       /'"_.-~^`'-.
//      \  _  /--'         `
//    ___)( )(___
//   (((__) (__)))    高山仰止, 景行行止; 虽不能至, 心向往之.
//                    High mountains, scenery. Although not to, the heart yearns for it.

/**
  * 数字增强类
  * @param param
  */
class SuperNumber(val param:String) {

  private val int_zero:Int = 0
  private val double_zero:Double = 0.0
  private val long_zero:Long = 0L

  def toIntOrElse0(): Int = {
    if (StringUtils.isBlank(param)) {
      int_zero
    } else {
      try {
        param.toInt
      } catch {
        case e: Exception => int_zero
      }
    }
  }

  def toDoubleOrElse0(): Double = {
    if (StringUtils.isBlank(param)) {
      double_zero
    } else {
      try {
        param.toDouble
      } catch {
        case ex: Exception => double_zero
      }
    }
  }

  def toLongOrElse0(): Long = {
    if (StringUtils.isBlank(param)) {
      long_zero
    } else {
      try {
        param.toLong
      } catch {
        case ex: Exception => long_zero
      }
    }
  }

  def toIntOrElse(default:Int): Int = {
    if (StringUtils.isBlank(param)) {
      default
    } else {
      try {
        param.toInt
      } catch {
        case ex: Exception => default
      }
    }
  }

  def toDoubleOrElse(default:Double): Double = {
    if (StringUtils.isBlank(param)) {
      default
    } else {
      try {
        param.toDouble
      } catch {
        case ex: Exception => default
      }
    }
  }

  def toLongOrElse(default:Long): Long = {
    if (StringUtils.isBlank(param)) {
      default
    } else {
      try {
        param.toLong
      } catch {
        case ex: Exception => default
      }
    }
  }

}

object SuperNumber {
  implicit def string2SuperNumber(param: String) = new SuperNumber(param)
}
